package com.hualan.sfzyproject.controller.User;


import com.hualan.sfzyproject.domain.*;
import com.hualan.sfzyproject.mapper.ConsultantsMapper;
import com.hualan.sfzyproject.service.ConsultantsService;
import com.hualan.sfzyproject.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/consultants")
public class ConsultantsController {
    @Autowired
    private ConsultantsService consultantsService;

    @Autowired
    private ConsultantsMapper consultantsMapper;

    // 查询所有咨询师(咨询师列表)（仅涉及咨询师表与等级表联查）

    @RequestMapping("/selectAllConsultantsAndLevel")
    public R selectAllConsultantsAndLevel(int pNum,int pSize){
        return R.ok(consultantsService.selectAllConsultantsAndLevel(pNum,pSize));
    }

// 查询所有地域（咨询筛选）
    @RequestMapping("/selectAllConsultantsAddress")
    public R selectAllConsultantsAddress(){
        return R.ok(consultantsService.selectAllConsultantsAddress());
    }



// 查询咨询师擅长（咨询筛选）
@RequestMapping("/selectAllAreas")
public R selectAllAreas(){
    return R.ok(consultantsService.selectAllAreas());
}

// 查询咨询师等级（咨询筛选）

@RequestMapping("/selectAllConsultantLevel")
public R selectAllConsultantLevel(){
    return R.ok(consultantsService.selectAllConsultantLevel());
}



// 查询咨询方式（咨询筛选）

@RequestMapping("/selectAllChargeStandardType")
public R selectAllChargeStandardType() {
    return R.ok(consultantsService.selectAllChargeStandardType());
}


//六表查
@RequestMapping("/selectDynamicQueries")
public R selectDynamicQueries(ConsultantsDynamicQueries dynamicQueries) {
    return R.ok(consultantsService.selectDynamicQueries(dynamicQueries));
}

@RequestMapping("/selectConsultantsByLevel")
public R selectConsultantsByLevel(int consultantLevelId){
    return R.ok(consultantsService.selectConsultantsByLevel(consultantLevelId));
    }

    @RequestMapping("/selectConsultantsByLike1")
    public R selectConsultantsByLike(String like){
        return R.ok(consultantsService.selectConsultantsByLike1(like));
        }

//    查询所有咨询师（根据id查询）(咨询师列表)（仅涉及咨询师表与等级表联查）

@RequestMapping("/selectConsultantsAndLevelById")
public R selectConsultantsAndLevelById(int pNum,int pSize,int[] consultantIds){
    return R.ok(consultantsService.selectConsultantsAndLevelById(pNum,pSize,consultantIds));
    }




//    查询所有咨询师（后台）

@RequestMapping("/selectAllConsultants")
    public List<Consultants> selectAllConsultants(int pageNo, int pageSize) {
        int offset = (pageNo - 1) * pageSize;
        return consultantsMapper.selectAllConsultants(offset, pageSize);

    }

//    模糊查询所有咨询师（后台）
    @RequestMapping("/selectConsultantsByLike")
    public List<Consultants> selectConsultantsByLike(int pageNo, int pageSize, String name) {
        int offset = (pageNo - 1) * pageSize;
        return consultantsMapper.selectConsultantsByLike(offset, pageSize, name);
    }


//    咨询师擅长表（后台-查询）
    @RequestMapping("/selectAreaById")
    public R selectAreaById(int consultantId){
        return R.ok(consultantsService.selectAreaById(consultantId));
    }


//    添加咨询师擅长(后台-添加)
@RequestMapping("/insertArea")
public R insertArea(@RequestBody ConsultantSpecializationArea consultantSpecializationArea){
    return R.ok(consultantsService.insertArea(consultantSpecializationArea));
}

//    删除咨询师擅长(后台-删除)
 @RequestMapping("/deleteAreaById")
 public R deleteAreaById( int consultantId,  int areaId){
     return R.ok(consultantsService.deleteAreaById(consultantId, areaId));
 }
//    添加咨询师时展示(后台-添加展示)
@RequestMapping("/selectAreaList2")
public R selectAreaList2(){
    return R.ok(consultantsService.selectAreaList2());
}

//查询收费标准管理（后台）
    @RequestMapping("/selectAllFeeManagement")
    public R selectAllFeeManagement(int consultantId){
        return R.ok(consultantsService.selectAllFeeManagement(consultantId));
    }
//    收费类型查询（后台）
    @RequestMapping("/selectChargeStandardType2")
    public R selectChargeStandardType2(){
        return R.ok(consultantsService.selectChargeStandardType());
    }

//    <!--    删除收费标准-->
    @RequestMapping("/deleteFeeManagementById")
    public R deleteFeeManagementById(int chargeTypeId,int consultantId){
        return R.ok(consultantsService.deleteFeeManagement(chargeTypeId,consultantId));
    }

//    添加收费标准
    @RequestMapping("/insertChargeStandardAssociation")
    public R insertChargeStandardAssociation( ChargeStandardAssociation chargeStandardAssociation){
        return R.ok(consultantsService.insertChargeStandardAssociation(chargeStandardAssociation));
    }







//===========================================================
    //咨询师登录
    @GetMapping("/login")
    public R consultantLogin(String name,String password) {

        int i = consultantsService.consultantLogin(name,password);
        Map<String, Object> consultantMap = new HashMap<>();

        if (i == 1){//登陆成功
            consultantMap.put("token", UUID.randomUUID());
            consultantMap.put("consultant",consultantsMapper.selectByName(name));
            return R.ok(consultantMap); //把对象传上去
        }else if (i == -1){
            return R.error("密码错误");
        }else{
            return R.error("用户名/手机号不存在");
        }
    }

    //咨询师注册
    @PostMapping("/register")
    public R register(@RequestBody Consultants consultant) throws ClassNotFoundException, SQLException {
        System.out.println(consultant);

        int i = consultantsService.consultantRegister(consultant);

        if (i > 0) {
            return R.success("注册成功");
        } else {
            return R.fail("注册失败");
        }

    }

    //动态sql修改咨询师信息
    @PutMapping
    public R updateUser(@RequestBody Consultants consultant){
        System.out.println("这是要进行修改的咨询师: "+consultant);
        int i = consultantsService.updateConsultant(consultant);
        if (i > 0) {
            return R.success("修改成功");
        } else {
            return R.fail("修改失败");
        }
    }

    //根据咨询师id查询咨询师信息
    @GetMapping("/selectByConsultantId")
    public R selectByConsultantId(Long consultantId){
        return R.ok(consultantsMapper.selectByPrimaryKey(consultantId));
    }

    //    咨询师详情  十表查询!
    @GetMapping("/selectConsultantsDetail")
    public R selectConsultantsDetail(Long consultantId){
        return R.ok(consultantsService.selectConsultantsDetail(consultantId));
    }



}
